<?php

/*
 * cakePHP Component using GT_CAS library
 * Please refer to GT_CAS in the app/Lib folder for usage information
 *
 * To enable this library for production use:
 * 1) Uncomment 'require_once' and each of the $this->cas->* methods below
 * 2) Comment/delete the commands between the <DEBUGGING> tags
 * 3) Ensure your setup meets the requirements for the GT_CAS libary (PEAR, HTTP_Request, NET_URL)
 * 4) Refer to the app/Config/cas_auth.php file to propperly configure your CAS client
 *
 * More information about CAS can be found here: http://share-it.gatech.edu/campus/csr/iam-users-group
 */

//require_once(APPLIBS.'GT_CAS.php');

include_once(APP.'/Config/cas_auth.php');

class CAS_AuthComponent extends Component{

	var $name = 'CAS_Auth';
	var $components = array('Session');
	var $cas;

	//<DEBUGGING>
	protected $_casDebugLogin = 'http://dl.dropbox.com/u/469438/fakeCAS/login.html'; 
	protected $_casDebugLogout = 'http://dl.dropbox.com/u/469438/fakeCAS/logout.html'; 
	//</DEBUGGING>

	function __construct(&$collection, $settings = array()) 
	{
		parent::__construct($collection, $settings);

		$config = Configure::read('cas_auth');
		//$this->cas = new GT_Authn_CAS($config['service'], $config['redirectAfter']);
		
		$this->processEnvironment();
	}

	/**
	 * This function checks to see if a user is logged in to CAS
	 */
	public function checkAuthentication() {
		//$this->cas->checkAuthentication();
	}

	public function forceAuthentication() {
		//$this->cas->checkAuthentication();

		//<DEBUGGING>
		if (!$this->isLoggedIn()) {
			$query=http_build_query(array('service'=> Configure::read('cas_auth.service')), '', '&');

			header('Location: '.$this->_casDebugLogin.'?'.$query);
			exit();
		}
		//</DEBUGGING>*/
	}


	public function casLogout() {
		//$this->cas->casLogout();

		//<DEBUGGING>
		return $this->Session->delete('GT.CAS.user');
		//</DEBUGGING>
	}

	public function localLogout() {
		//$this->cas->localLogout();

		//<DEBUGGING>
		return $this->Session->delete('GT.CAS.user');
		//</DEBUGGING>
	}


	public function isLoggedIn() {
		//$this->cas->isLoggedIn();

		//<DEBUGGING>
		return $this->Session->check('GT.CAS.user');
		//</DEBUGGING>
	}

	public function getGTAccount() {
		//return $this->cas->getGTAccount();

		//<DEBUGGING>
		if ($this->isLoggedIn()) {
			return $this->Session->read('GT.CAS.user');
		}
		return null;
		//</DEBUGGING>
	}

	/**
	 * This function looks at the current environment and takes action if necessary
	 * Function will see if the CAS 'ticket' variable is present and verify it accordingly
	 */
	protected function processEnvironment() {
		//$this->cas->processEnviroment();

		//<DEBUGGING
		if (array_key_exists('ticket',$_GET)){
			if ($user = $this->verifyServiceTicket($_GET['ticket'])) {
				$this->Session->write('GT.CAS.user', $user);
			}
			else {
				//ticket was bad for some reason.  Nuke the session information
				$this->localLogout();
			}
		}
		//</DEBUGGING>
	}

	public function verifyServiceTicket($ticket) {
		//return $this->cas->verifyServiceTicket($ticket);

		//<DEBUGGING>
		return $ticket;
		//</DEBUGGING>
	}
}

?>
